﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WyUnity.Data.Query;
using WyUnity.Data.TableMapper;

namespace RFID_Interface.DTO
{
    public class BillListResponse
    {
        public char Result { get; set; }
        public string ErrMsg { get; set; }

        public List<BillInfo> BillList { get; set; }

        public static BillListResponse Get(string stocknumber, int billtype, DateTime datebegin, DateTime dateend)
        {
            try
            {
                if (billtype <= 0 || billtype > 45) throw new Exception("单据类型错误");

                List<BillInfo> list;

                using (var db = DB.Open())
                {
                    var where = BillInfo.pBillType == billtype;
                    if (!string.IsNullOrEmpty(stocknumber))
                    {
                        where &= (BillInfo.pSendStockNum == stocknumber | BillInfo.pRecieveStockNum == stocknumber);
                    }
                    if (datebegin.Year >= 2000)
                    {
                        where &= BillInfo.pDate >= datebegin;
                    }
                    if (dateend.Year <= 2050)
                    {
                        where &= BillInfo.pDate <= dateend;
                    }

                    list = db.QueryList<BillInfo>(where, null);
                }

                return new BillListResponse { Result = 'S', BillList = list };
            }
            catch (Exception ex)
            {
                return new BillListResponse { Result = 'F', ErrMsg = "系统错误.\n" + ex.Message };
            }
        }
    }

    public class BillInfo : ViewBase<BillInfo>
    {
        [Map]
        public string BillNo { get; set; }
        [Map]
        public string Remark { get; set; }
        [Map]
        public DateTime Date { get; set; }
        [Map]
        public string Biller { get; set; }
        [Map]
        public int Status { get; set; }
        [Map]
        public string SendStockNum { get; set; }
        [Map]
        public string RecieveStockNum { get; set; }
        [Map]
        public int BillType { get; set; }

        public static FieldBase pSendStockNum = new Field<BillInfo>(p => p.SendStockNum);
        public static FieldBase pRecieveStockNum = new Field<BillInfo>(p => p.RecieveStockNum);
        public static FieldBase pDate = new Field<BillInfo>(p => p.Date);
        public static FieldBase pBillType = new Field<BillInfo>(p => p.BillType);

        public override string GetSQLClip()
        {
            return @"
SELECT  isb.FBillNo BillNo ,
        isb.FTranType BillType ,
        isb.FNote Remark ,
        isb.FDate [Date] ,
        tu.FName Biller ,
        isb.FStatus [Status] ,
        CASE WHEN isb.FTranType IN ( 21, 26, 28, 29, 43 ) THEN ts.FNumber
             WHEN isb.FTranType IN ( 24, 41 ) THEN ts2.FNumber
             ELSE ts2.FNumber
        END SendStockNum ,
        CASE WHEN isb.FTranType IN ( 1, 2, 5, 6, 40, 41 ) THEN ts.FNumber
             ELSE ''
        END RecieveStockNum
FROM    ICStockBill AS isb
        LEFT JOIN t_Stock AS ts ON isb.FDCStockID = ts.FItemID
        LEFT JOIN t_Stock AS ts2 ON isb.FSCStockID = ts.FItemID
        LEFT JOIN t_User AS tu ON isb.FBillerID = tu.FUserID
";


        }
    }
}